﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace OfficeClip.OpenSource.StyleFix.Drivers
{
    class MaintainabilityRules : StyleCopRules
    {

        private string GetDefaultAccessor(bool nested, string member)
        {
            if (!nested)
            {
                switch (member)
                {
                    case "enum": 
                        return "public";
                    case "interface":
                    case "class":
                    case "struct":
                    case "delegate":
                        return "internal";
                }
            }
            else
            {
                switch (member)
                {
                    case "enum":
                    case "interface":
                        return "public";
                    case "constructor":
                        return "protected";
                    case "class":
                    case "struct":
                    case "delegate":
                    case "method":
                    case "field":
                        return "private";
                }
            }
            return string.Empty;
        }

        internal void SA1400_AccessModifierMustBeDeclared(ref List<SFWorkingLine> workingLines)
        {
            CodeUtils.CalculateNestingLevels(workingLines);

            foreach(SFWorkingLine workingLine in workingLines)
            {
                bool isNested = workingLine.NestingLevel > 0;

                if (IsLineViolated(workingLine, "SA1400"))
                {
                    SAObject saob = workingLine.Violations.Find(sao => sao.ErrorId == "SA1400");

                    string target = saob.Description.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)[1];

                    string whitespace = Regex.Match(workingLine.Line, @"\s*").Value;

                    workingLine.Line = whitespace + GetDefaultAccessor(isNested, target) + " " + workingLine.Line.TrimStart();
                }
            }
        }
    }
}
